package com.allwees.bs.module.oms.dao;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.allwees.bs.module.oms.entity.OrderSkuSnap;
import com.allwees.core.dao.mybatis.dao.BaseDao;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public interface OrderSkuSnapDao extends BaseDao<OrderSkuSnap> {

    default Map<String, OrderSkuSnap> findByOrderItemUuids(Set<String> orderItemUuids) {
        QueryWrapper<OrderSkuSnap> wrapper = new QueryWrapper<>();
        wrapper.in("order_item_uuid", orderItemUuids);
        List<OrderSkuSnap> orderSkuSnaps = selectList(wrapper);
        if (CollectionUtils.isEmpty(orderSkuSnaps)) {
            return new HashMap<>(0);
        }
        return orderSkuSnaps.stream().collect(Collectors.toMap(OrderSkuSnap::getOrderItemUuid, o->o));
    }

    default List<OrderSkuSnap> findByOrderUuid(String orderUuid) {
        QueryWrapper<OrderSkuSnap> wrapper = new QueryWrapper<>();
        wrapper.eq("order_uuid", orderUuid);
        return selectList(wrapper);
    }

    default OrderSkuSnap findOne(String orderUuid, String skuUuid) {
        QueryWrapper<OrderSkuSnap> wrapper = new QueryWrapper<>();
        wrapper.eq("order_uuid", orderUuid);
        wrapper.eq("sku_uuid", skuUuid);
        return selectOne(wrapper);
    }

    default OrderSkuSnap findByOrderItemUuid(String key){
        return findOneByField("order_item_uuid", key);
    }

    default List<OrderSkuSnap> findByOrderUuids(Set<String> orderUuids){
        if(CollectionUtil.isEmpty(orderUuids)){
            return new ArrayList<>(0);
        }
        QueryWrapper<OrderSkuSnap> wrapper = new QueryWrapper<>();
        wrapper.in("order_uuid", orderUuids);
        return selectList(wrapper);
    }
}

